﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class SetTeeFittingForm : System.Windows.Forms.Form
	{
		public SetTeeFittingForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{9D680094-7784-4397-9361-A0DD53CB8BC4}", null);
			base.Icon = Resource.YArchitechIcon;
		}

		private void SetTeeFittingForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.InitialInterface();
		}

		private void InitialInterface()
		{
			this.hrTee = this.userSetting.GetHConnRPipeTee();
			if (this.hrTee == "顺水三通")
			{
				this.SelectButton(this.btnWellTee);
			}
			else if (this.hrTee == "水管三通")
			{
				this.SelectButton(this.btnCommon);
			}
			else
			{
				this.SelectButton(this.btnWellTee);
			}
			this.hrAlign = this.userSetting.GetHConnRPipeAlign();
			string a = this.hrAlign;
			if (!(a == "无"))
			{
				if (!(a == "横管"))
				{
					if (!(a == "立管"))
					{
						if (!(a == "大管径"))
						{
							this.rdbDirect.Checked = true;
						}
						else
						{
							this.rdbAlignMPipe.Checked = true;
						}
					}
					else
					{
						this.rdbAlignRPipe.Checked = true;
					}
				}
				else
				{
					this.rdbAlignHPipe.Checked = true;
				}
			}
			else
			{
				this.rdbDirect.Checked = true;
			}
			this.hrDiameter = this.userSetting.GetHConnRPipeDiameter();
			this.hytbMidPipeDiameter.Text = ((this.hrDiameter == "") ? "15" : this.hrDiameter);
		}

		private void SelectButton(Button button)
		{
			button.PerformClick();
			button.Select();
			button.Focus();
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			this.userSetting.WriteHConnRPipeTee(this.hrTee);
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.hrTee = this.userSetting.GetHConnRPipeTee();
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnWellTee_Click(object sender, EventArgs e)
		{
			System.Drawing.Color backColor = this.groupBox1.BackColor;
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			foreach (object obj in this.groupBox1.Controls)
			{
				Button button2 = obj as Button;
				if (button2 != null)
				{
					button2.BackColor = backColor;
					if (button2 == button)
					{
						if (button2.Name == "btnWellTee")
						{
							button2.BackColor = System.Drawing.Color.LightSkyBlue;
							this.hrTee = "顺水三通";
						}
						if (button2.Name == "btnCommon")
						{
							button2.BackColor = System.Drawing.Color.LightSkyBlue;
							this.hrTee = "水管三通";
						}
					}
				}
			}
		}

		private void SetTeeFittingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.userSetting.WriteHConnRPipeTee(this.hrTee);
			this.userSetting.WriteHConnRPipeAlign(this.hrAlign);
			this.userSetting.WriteHConnRPipeDiameter(this.hrDiameter);
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnCommon_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Clicks == 2)
			{
				this.userSetting.WriteHConnRPipeTee(this.hrTee);
				base.DialogResult = DialogResult.OK;
			}
		}

		private void rdbDirect_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbAlignHPipe.Checked)
			{
				this.hrAlign = "横管";
			}
			if (this.rdbAlignRPipe.Checked)
			{
				this.hrAlign = "立管";
			}
			if (this.rdbAlignMPipe.Checked)
			{
				this.hrAlign = "大管径";
			}
			if (this.rdbDirect.Checked)
			{
				this.hrAlign = "无";
			}
		}

		public string GetTeeName()
		{
			return this.hrTee;
		}

		public int GetConnStyle()
		{
			int result = 0;
			if (this.rdbAlignHPipe.Checked)
			{
				this.hrAlign = "横管";
			}
			if (this.rdbAlignRPipe.Checked)
			{
				this.hrAlign = "立管";
			}
			if (this.rdbAlignMPipe.Checked)
			{
				this.hrAlign = "大管径";
			}
			if (this.rdbDirect.Checked)
			{
				this.hrAlign = "无";
			}
			string a = this.hrAlign;
			if (!(a == "无"))
			{
				if (!(a == "横管"))
				{
					if (!(a == "立管"))
					{
						if (a == "大管径")
						{
							result = 3;
						}
					}
					else
					{
						result = 1;
					}
				}
				else
				{
					result = 2;
				}
			}
			else
			{
				result = 0;
			}
			return result;
		}

		public int GetConnDiameter()
		{
			return int.Parse(this.hrDiameter);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetTeeFittingForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			SetTeeFittingForm.SendMessage(HengLianLiPipeNew.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			SetTeeFittingForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SetTeeFittingForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SetTeeFittingForm_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				SetTeeFittingForm.SendMessage(HengLianLiPipeNew.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void SetTeeFittingForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				SetTeeFittingForm.SendMessage(HengLianLiPipeNew.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void SetTeeFittingForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void hytbMidPipeDiameter_Validating(object sender, CancelEventArgs e)
		{
			if (!this.regex2.IsMatch(this.hytbMidPipeDiameter.Text.Trim()))
			{
				YJKMessageBox.Show("输入数字格式不正确,直径只能为正整数，且不能为空！");
				e.Cancel = true;
				this.hytbMidPipeDiameter.Undo();
				return;
			}
			this.hrDiameter = this.hytbMidPipeDiameter.Text.Trim();
		}

		private void hytbMidPipeDiameter_TextChanged(object sender, EventArgs e)
		{
			if (!this.regex2.IsMatch(this.hytbMidPipeDiameter.Text.Trim()))
			{
				YJKMessageBox.Show("输入数字格式不正确,直径只能为正整数，且不能为空！");
				this.hytbMidPipeDiameter.Undo();
				return;
			}
			this.hrDiameter = this.hytbMidPipeDiameter.Text.Trim();
		}

		private string hrTee = "";

		private string hrAlign = "";

		private string hrDiameter = "";

		private HVACUserSetting userSetting = new HVACUserSetting();

		private ControlInfoManager m_thisManger;

		private Regex regex2 = new Regex("^[0-9]*[1-9][0-9]*$");
	}
}
